<?php

namespace app\admin\controller\shopro\commission;

use app\admin\controller\shopro\Common;
use app\admin\model\Admin;
use app\admin\model\shopro\Pclog as PclogModel;
use app\admin\model\User;

//平台相关资金来往
class Pclog extends Common
{
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 查看
     */
    public function index()
    {
        if (!$this->request->isAjax()) {
            $exportConfig = (new \addons\shopro\library\Export())->getConfig();
            $this->assignconfig("save_type", $exportConfig['save_type'] ?? 'download');
            return $this->view->fetch();
        }
        $where=[];
        if($this->request->param("search")){
            $search=json_decode($this->request->param("search"),true);
            if(isset($search["user_id"])) $where["user_id"]=["=",$search["user_id"][0]];
            if(isset($search["type"])) $where["type"]=["=",$search["type"][0]];
            if(isset($search["user.nickname"])) $where["user_info.nickname"]=["like", '%'.$search["user.nickname"][0].'%'];
            if(isset($search["user.mobile"])) $where["user_info.mobile"]=["like",'%'.$search["user.mobile"][0].'%'];
            if(isset($search["createtime"])){
                $time=explode(" - ",$search["createtime"][0]);
                $where["pclog.createtime"]=["between",[strtotime($time[0]),strtotime($time[1])]];
            }
        }
        $data = PclogModel::field("pclog.*,from_unixtime(pclog.createtime, '%Y-%m-%d %H:%i:%s') createtime")
            ->alias("pclog")
            ->with("user")
            ->join("user user_info","user_info.id=pclog.user_id","left")
            ->where($where)
            ->order("pclog.createtime desc,pclog.id desc")
            ->paginate($this->request->param('list_rows', 10));
        $this->success('获取成功', null, $data);
    }
    /**
     * 导出
     * */
    public function export()
    {
        $cellTitles = [
            'id' => 'ID',
            'user.id' => '用户ID',
            'user.nickname' => '用户昵称',
            'user.mobile' => '用户手机号码',
            'money' => '金额',
            'pv_price' => '订单pv值/充正金额',
            'type_text' => '类型',
            'order_id' => '关联订单id',
            'ext.order_sn' => '订单编号',
            'createtime' => '创建时间',
        ];

        // 数据总条数
        $where=[];
        if($this->request->param("search")){
            $search=json_decode($this->request->param("search"),true);
            if(isset($search["user_id"])) $where["user_id"]=["=",$search["user_id"][0]];
            if(isset($search["type"])) $where["type"]=["=",$search["type"][0]];
            if(isset($search["user.nickname"])) $where["user_info.nickname"]=["like", '%'.$search["user.nickname"][0].'%'];
            if(isset($search["user.mobile"])) $where["user_info.mobile"]=["like",'%'.$search["user.mobile"][0].'%'];
            if(isset($search["createtime"])){
                $time=explode(" - ",$search["createtime"][0]);
                $where["pclog.createtime"]=["between",[$time[0],$time[1]]];
            }
        }
        $total = PclogModel::field("pclog.*,from_unixtime(pclog.createtime, '%Y-%m-%d %H:%i:%s') createtime")
            ->alias("pclog")
            ->with("user")
            ->join("user user_info","user_info.id=pclog.user_id","left")
            ->where($where)->count();
        if ($total <= 0) {
            $this->error('导出数据为空');
        }
        $export = new \addons\shopro\library\Export();
        $params = [
            'file_name' => '平台相关明细列表',
            'cell_titles' => $cellTitles,
            'total' => $total,
            'is_sub_cell' => false,
        ];

        $result = $export->export($params, function ($pages) use ($where, $total) {
            $datas = PclogModel::field("pclog.*,from_unixtime(pclog.createtime, '%Y-%m-%d %H:%i:%s') createtime")
                ->alias("pclog")
                ->with("user")
                ->join("user user_info","user_info.id=pclog.user_id","left")
                ->where($where)
                ->order("pclog.createtime desc,pclog.id desc")
                ->limit((($pages['page'] - 1) * $pages['list_rows']), $pages['list_rows'])
                ->select();
            $datas = collection($datas);
            $newDatas = [];
            foreach ($datas as &$item) {
                $data = [
                    // 订单表字段
                    'id' => $item['id'],
                    'user.id' =>$item["user"]["id"]??0,
                    'user.nickname' =>$item["user"]["nickname"]??'',
                    'user.mobile' =>"@".$item["user"]["mobile"]??'',
                    'money' =>$item["money"],
                    'pv_price' =>$item["pv_price"],
                    'type_text' =>$item["type_text"],
                    'order_id' =>$item["order_id"],
                    'ext.order_sn' =>$item["ext"]["order_sn"]??'',
                    'createtime' =>$item["createtime"],
                ];
                $newDatas[] = $data;
            }
            return $newDatas;
        });

        $this->success('导出成功' . (isset($result['file_path']) && $result['file_path'] ? '，请在服务器: “' . $result['file_path'] . '” 查看' : ''), null, $result);
    }
}
